package com.htjc.integral.exchange.service.impl;

import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;
import com.alibaba.fastjson.JSON;

import org.springframework.stereotype.Service;

import com.htjc.integral.common.util.Constance;
import com.htjc.integral.exchange.service.IPartnerRegisterService;
import com.htjc.integral.exchange.utils.CommonTools;
import com.htjc.integral.exchange.utils.SgccMallUtil;
import com.htjc.integral.model.exchange.EcPartner;
import com.sgcc.sip.access.rest.SipRestAccessAgent;
import com.sgcc.sip.access.rest.SipRestAccessAgentFactory;
import org.apache.commons.lang.StringUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "exchangeLogger")
@Service
public class PartnerRegisterServiceImpl  implements IPartnerRegisterService {
	@Override
	public EcPartner getPartnerByUserId(String userid){
		return this.getPartnerByIdMongodb(userid);
	}
	
	/**
	 * Mongodb
	 * 查询用户信息 根据id
	 * @param partnerId
	 * @return
	 * @throws Exception 
	 */
	private  EcPartner getPartnerByIdMongodb(String partnerId){
		
		EcPartner ecPartner = null;
		try {	
			String uscBaseUrl = SgccMallUtil.getInstance().getConfValue("site.sip.H5.uscBaseUrl");
			log.info("=================uscBaseUrl="+uscBaseUrl+"================================");
			//http://sip.esgmallt.com/sip/authz/gettoken
			SipRestAccessAgent agent=egent_Black();
		    JSONObject uscInfo = new JSONObject();
			uscInfo.put("tenant", Constance.TENANT_CODE_PARAM_VALUE);
			uscInfo.put("member", Constance.APP_CODE_PERSONAL);
			
			/// String ticketAuthUrl = SgccMallUtil.getInstance().getConfValue("site.usc.server.url");
				Map<String,String> params=new HashMap<String,String>();
				params.put("uscInfo",uscInfo.toString());
				params.put("userId",partnerId);
				String	resultJson = null;
				try {
				//	String authUrl=uscBaseUrl+"/malllogin/getuserinfoNoToken";
					String authUrl=uscBaseUrl+"/user/getuserinfoNoToken";
					resultJson=agent.post(authUrl,params);
					//System.out.println("输出一下接口返回来的数据看一下"+resultJson);
					log.info("=================输出一下接口返回来的数据看一下="+resultJson+"================================");
					
					 if(StringUtils.equals(Constance.RE_CODE_0100,
					        	JSON.parseObject(resultJson).getJSONObject("srvrt").getString("resultCode"))){
						 log.info("查询的用户为空+++++++"+ecPartner);
						 //用户为null
						 return ecPartner;
					 }
				//	resultJson = RestUtils.post(ticketAuthUrl+"/malllogin/getuserinfoNoToken",params);
				} catch (Exception e) {
					log.info("usc-server调用日志:"+e);
				}
			
	           if(StringUtils.equals(Constance.RE_CODE_0000,
	        	JSON.parseObject(resultJson).getJSONObject("srvrt").getString("resultCode"))){
	        	   CommonTools commonTools = new CommonTools();
	          	 Object object =JSON.parseObject(resultJson).getJSONObject("bizrt").getJSONObject("userInfo");
	       		if(object!=null){
	       			 ecPartner = commonTools.userInfoJsonToPartner(JSON.toJSONString(object));
	       		  }	 
	           }
		} catch (Exception e) {
			e.printStackTrace();
			log.error("根据id查询用户信息异常", e);
		}
		
		
          return ecPartner;
	}
	
	
	
	public static SipRestAccessAgent egent_Black(){
		String ceshi  = "shujuku";
		String url = SgccMallUtil.getInstance().getConfValue("site.sip.H5.url");
		String account = SgccMallUtil.getInstance().getConfValue("site.sip.H5.account");
		String pwd = SgccMallUtil.getInstance().getConfValue("site.sip.H5.pwd");
		String secretKey = SgccMallUtil.getInstance().getConfValue("site.sip.H5.secretKey");
		String uscBaseUrl = SgccMallUtil.getInstance().getConfValue("site.sip.H5.uscBaseUrl");

		SipRestAccessAgentFactory factory=
				new SipRestAccessAgentFactory(url,account,pwd,secretKey,"1111");
		SipRestAccessAgent agent=factory.getInstance();
		
		return agent;
	}
}
